/*
 * @Author: wanglianxin 532445330@qq.com
 * @Date: 2023-11-09 13:58:30
 * @LastEditors: wanglianxin 532445330@qq.com
 * @LastEditTime: 2023-11-19 22:35:36
 * @FilePath: /src/pages/Markdown/index.ts
 * @Description: 公共组件入口
 */
//markdown解析
import MarkdownIt from "markdown-it";
import hljs from "highlight.js"; // https://highlightjs.org/
import "highlight.js/styles/github.css";
import "./bootstrap.css";
//引入公共组件
import Pagination from "../../publicComponents/Pagination/README.md?raw";
import PublicForm from "../../publicComponents/PublicForm/README.md?raw";
import SearchLayout from "../../publicComponents/SearchLayout/README.md?raw";
import Authorization from "../../publicComponents/Authorization/README.md?raw";
import CollectImages from "../../publicComponents/CollectImages/README.md?raw";
function replaceImg(md: string, name: string): string {
  return md.replace(/\!\[\]\(.*\)/g, function (result) {
    result = result
      .substring(result.indexOf("(") + 1, result.indexOf(")"))
      .replace(/^\.\//, "");
    return (
      "![](" +
      new URL(`../../publicComponents/${name}/${result}`, import.meta.url)
        .href +
      ")"
    );
  });
}
const markdown = new MarkdownIt({
  html: true,
  linkify: true,
  typographer: true,
  highlight: function (str: string, lang: string) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return (
          '<pre class="hljs"><code>' +
          hljs.highlight(lang, str, true).value +
          "</code></pre>"
        );
      } catch (__) {}
    }

    return (
      '<pre class="hljs"><code>' +
      markdown.utils.escapeHtml(str) +
      "</code></pre>"
    );
  },
});
export interface Component {
  name: string;
  key: string;
  markdown: string;
}
function createMd(mdContent: string, mdName: string) {
  return markdown.render(replaceImg(mdContent, mdName));
}
export const components: Component[] = [
  {
    name: "页码组件",
    key: "Pagination",
    markdown: createMd(Pagination, "Pagination"),
  },
  {
    name: "表单组件",
    key: "PublicForm",
    markdown: createMd(PublicForm, "PublicForm"),
  },
  {
    name: "布局（带搜索）组件",
    key: "SearchLayout",
    markdown: createMd(SearchLayout, "SearchLayout"),
  },
  {
    name: "授权弹窗组件",
    key: "Authorization",
    markdown: createMd(Authorization, "Authorization"),
  },
  {
    name: "采集图像组件",
    key: "CollectImages",
    markdown: createMd(CollectImages, "CollectImages"),
  },
];
